package com.nc.cloud.authority.client.interceptor;


import com.nc.cloud.authority.client.config.UserAuthConfig;
import com.nc.cloud.common.context.BaseContextHandler;
import lombok.extern.java.Log;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description OKhttp拦截器
 * @ClassName OkHttpTokenInterceptor
 * @Author NC—Cloud
 * @date 2020.03.28 16:10
 */
@Component
@Log
public class OkHttpTokenInterceptor implements Interceptor {

    @Autowired
    @Lazy
    private UserAuthConfig userAuthConfig;


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request newRequest = chain.request()
                .newBuilder()
                .header(userAuthConfig.getTokenHeader(), BaseContextHandler.getToken())
                .build();

        Response response = chain.proceed(newRequest);

        return response;
    }
}
